﻿using MiniFox.Platform.Applications;
using MiniFox.Platform.Scripting;
using MiniFox.Scripting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MiniFox.Platform.Applications.Scripting
{
    /// <summary>
    /// 
    /// </summary>
    public class SystemIdentityFunction : StateFunctionBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public SystemIdentityFunction(IFunctionSetting setting) : base(setting)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public SystemIdentityFunction(string name) : base(name)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        protected override object Execute(IDictionary<string, object> arguments)
        {
            string field = arguments.Count <= 0 ? string.Empty : arguments["type"].ToString();

            Type type = this.StateManagement.GetType();

            PropertyInfo propertyInfo = type.GetProperty("CurrentIdentity");
            if (propertyInfo == null)
                return null;

            var identity = propertyInfo.GetValue(this.StateManagement);

            Type identityType = identity.GetType();

            var property = identityType.GetProperty(field);
            if (property == null)
                return null;

            var val = property.GetValue(identity);

            return val;
        }
    }
}
